package com.xxl.sso.server.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import com.icom.sso.mapper.ProjectMapper;
import com.icom.sso.util.FileUtil;
import com.icom.sso.util.RetKit;
import com.xxl.sso.core.entity.PageData;
import com.xxl.sso.server.core.model.Project;
import com.xxl.sso.server.core.model.SysUser;
import com.xxl.sso.server.service.ProjectService;

@Service
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	ProjectMapper projectMapper;

	@Override
	public List<Project> findProjects() {
		// TODO Auto-generated method stub
		List<Project> list = projectMapper.findProjects();
		return list;
	}

	@Override
	public PageData<Project> findPageProject(int page, int rows) {
		// TODO Auto-generated method stub
		List<Project> listAll = projectMapper.findProjects();

		// 从第几条数据开始
		int firstIndex = (page - 1) * rows;
		// 到第几条数据结束
		int lastIndex = page * rows;
		// 截取查询页的数据
		List<Project> list = new ArrayList<>();
		if (lastIndex <= listAll.size()) {
			list = listAll.subList(firstIndex, lastIndex);
		} else {
			list = listAll.subList(firstIndex, listAll.size());
		}

		return new PageData<Project>(list, page, rows, listAll.size());
	}

	public RetKit<String> uploadFile(MultipartFile file, String uploadPath) {
		RetKit<String> ret = new RetKit<String>(null);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date day = new Date();
		// TODO Auto-generated method stub
		if (file != null && !file.isEmpty()) {
			// 获取文件名称,包含后缀
			String fileName = file.getOriginalFilename();

			try {
				// 该方法是对文件写入的封装，在util类中，导入该包即可使用，后面会给出方法
				FileUtil.fileupload(file.getBytes(), uploadPath, sdf.format(day) + "_" + fileName);
				ret.setSuccess();
				ret.setData(sdf.format(day) + "_" + fileName);
				return ret;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret.setFail();
				return ret;
			}

		} else {
			ret.setFail();
			return ret;
		}
	}

	@Override
	public RetKit<String> saveProject(Project project) {
		RetKit<String> ret = new RetKit<String>(null);
		// TODO Auto-generated method stub
		try {
			int row = projectMapper.insertProject(project);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ret.setFail();
			ret.setMsg(e.toString());
		}

		return ret;
	}

	@Override
	public Project findProjectById(String id) {
		// TODO Auto-generated method stub
		Project project = projectMapper.findProjectById(id);
		return project;
	}

	@Override
	public RetKit<String> changeStatus(String id, String status) {
		RetKit<String> ret = new RetKit<String>(null);
		Project project = projectMapper.findProjectById(id);

		if (project == null) {
			ret.setFail();
			ret.setMsg("参数错误，查找不到对应的项目信息！");
			return ret;
		}

		try {
			int row = projectMapper.changeStatus(id, status);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ret.setFail();
			ret.setMsg("修改状态出现异常！");
		}

		return ret;
	}

	@Override
	public RetKit<Project> changeProjectInfo(String id) {
		// TODO Auto-generated method stub
		RetKit<Project> ret = new RetKit<Project>(null);

		Project project = projectMapper.findProjectById(id);

		if (project == null) {
			ret.setFail();
			ret.setMsg("差找不到对应的项目信息！");
		} else {
			ret.setData(project);
		}

		return ret;
	}

	@Override
	public RetKit<String> saveChangeInfo(Project project) {
		// TODO Auto-generated method stub
		RetKit<String> ret = new RetKit<String>(null);

		try {
			int row = projectMapper.saveChangeInfo(project);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ret.setFail();
			ret.setMsg("修改信息出现异常！");
		}

		return ret;
	}

	@Override
	public RetKit<String> delProject(String id) {
		// TODO Auto-generated method stub
		RetKit<String> ret = new RetKit<String>(null);

		try {
			int row = projectMapper.delProjt(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ret.setFail();
			ret.setMsg("修改信息出现异常！");
		}

		return ret;
	}

}
